package com.controller;

import com.dao.HolidayMapper;
import com.dao.SalaryMapper;
import com.pojo.Holiday;
import com.pojo.Salary;
import com.pojo.User;
import com.Utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 请假控制器
 * @author fzp
 */
@Controller
public class HolidayController {

    @Autowired
    HolidayMapper holidayMapper;

    @Autowired
    SalaryMapper salaryMapper;

    @ResponseBody
    @RequestMapping("holiday/getAllHoliday")
    public List<Holiday> getAllHoliday(){
        return holidayMapper.getAllHoliday();
    }

    @PostMapping("holiday/addHoliday")
    public String addHoliday(Model model, Holiday holiday){
        holiday.setH_submit(DateUtil.getCurrentTime());
        int result=holidayMapper.addHoliday(holiday);
        if (result>=1){
            model.addAttribute("msg","提交成功");
        }else {
            model.addAttribute("msg","提交失败");
        }
        return "holiday/addHoliday";
    }


    @RequestMapping("holiday/toaddHoliday")
    public String toaddHoliday(){
        return "holiday/addHoliday";
    }


    @RequestMapping("holiday/delHoliday")
    public String delHoliday(Model model,int h_id){
        int result=holidayMapper.delHoliday(h_id);
        if (result>0){
            model.addAttribute("msg","成功");
        }else {
            model.addAttribute("msg","失败");
        }
        return "holiday/checkHolidays";
    }


    @RequestMapping("holiday/checkHolidays")
    public String tocheckHoliday(Model model){
        model.addAttribute("holidaylist",holidayMapper.getAllCheckHoliday());
        return "holiday/checkHolidays";
    }

    @RequestMapping("holiday/tocheck")
    public String tocheck(Model model,int h_id){
        model.addAttribute("holiday",holidayMapper.getHolidayById(h_id));
        return "holiday/checkHoliday";
    }


    @RequestMapping("holiday/checkHoliday")
    public String checkHoliday(Model model,Holiday holiday){
        Subject subject = SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user= (User) session.getAttribute("user");
        holiday.setH_checkdate(DateUtil.getCurrentTime());
        holiday.setH_checkman(user.getU_username());
        if (holiday.getH_statu()==2){
            List<Salary> salary=salaryMapper.getAllSalaryByUid(holiday.getU_id());
        }
        int result=holidayMapper.updateHoliday(holiday);
        if (result>=1){
            model.addAttribute("msg","提交成功");
        }else {
            model.addAttribute("msg","提交失败");
        }
        return "holiday/checkHoliday";
    }
    @RequestMapping("holiday/updateHoliday")
    public int updateHoliday(Holiday holiday){
        return holidayMapper.updateHoliday(holiday);
    }

    @ResponseBody
    @RequestMapping("holiday/getHolidayById")
    public Holiday getHolidayById(int id){
        return holidayMapper.getHolidayById(id);
    }

    @RequestMapping("holiday/holidayIndex")
    public String toHoliday(Model model){
        model.addAttribute("holidaylist",holidayMapper.getAllHoliday());
        return "holiday/holidayIndex";
    }

    @RequestMapping("holiday/holidayDetails")
    public String holidaydetails(Model model,int h_id){
        Holiday holiday=holidayMapper.getHolidayById(h_id);
        model.addAttribute("holiday",holiday);
        return "holiday/holidayDetails";
    }

    @RequestMapping("holiday/myHoliday")
    public String myHoliday(Model model,int u_id){
        model.addAttribute("holidaylist",holidayMapper.getHolidayById(u_id));
        return "holiday/myHoliday";
    }


}
